package com.qzboot.common.support;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.Serializable;

/**
 * @author zengzhihong
 * @date 2019-04-17
 */
@Data
public class WebResult<T> implements Serializable {

    private static final long serialVersionUID = 1705570983990061274L;

    private boolean success = true;

    private String message = "成功";

    private Integer code = 0;

    private T data;

    private long timestamp = System.currentTimeMillis();

    public WebResult() {
    }

    public String toJson() {
        return JSON.toJSONString(this);
    }


    public static WebResult<Object> error(String msg) {
        return error(500, msg);
    }

    public static WebResult<Object> error(int code, String msg) {
        WebResult<Object> r = new WebResult<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static WebResult<Object> success() {
        return new WebResult<>();
    }

    public static WebResult<Object> success(Object data) {
        WebResult<Object> r = new WebResult<>();
        r.setSuccess(true);
        r.setData(data);
        return r;
    }
}
